package com.yangzc.lion.common.tag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页数据存储对象
 *
 */
@SuppressWarnings("serial")
public class Page<T> implements Serializable {

	private int totalPage = 0; // 页数
	private int totalSize = 0; // 记录数
	private int pageNo = 0; // 当前页
	private int offset = 1; // 索引值
	public static final int DEFAULT_PAGESIZE = 15;
	private int pageSize = DEFAULT_PAGESIZE; // 显示数
	private List<T> rows;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public void excecute() {
		if (this.pageNo <= 0)
			this.pageNo = 1;
		if (this.pageSize <= 0)
			this.pageSize = DEFAULT_PAGESIZE;
		int start = 1;
		if (this.totalSize <= (this.pageNo - 1) * this.pageSize) {
			if (this.totalSize % this.pageSize == 0)
				this.pageNo = (this.totalSize / this.pageSize);
			else {
				this.pageNo = (this.totalSize / this.pageSize + 1);
			}
		}
		if (this.pageNo <= 0)
			this.pageNo = 1;
		start = this.pageSize * (this.pageNo - 1);
		int totalpage = 1;

		if (this.totalSize % this.pageSize == 0)
			totalpage = this.totalSize / this.pageSize;
		else {
			totalpage = this.totalSize / this.pageSize + 1;
		}
		this.totalPage = totalpage;
		this.offset = start;
	}

	public static <T> Page<T> getEmptyPage() {
		Page<T> page = new Page<T>();
		page.setPageSize(DEFAULT_PAGESIZE);
		page.setRows(new ArrayList<T>());
		page.setTotalPage(1);
		page.setPageNo(1);
		return page;
	}

}